//
//  ViewController.m
//  UI29__Block
//
//  Created by dllo on 16/1/20.
//  Copyright © 2016年 dilo. All rights reserved.
//

#import "ViewController.h"
#import "SecondViewController.h"
#import "TouchView.h"
#import "NetworkingHandler.h"
/** 函数声明 */

int sum(int, int);

/** 函数实现 */
int sum(int x, int y){
    
    
    return x+y;
    
    
}
int sub(int x,int y){
    
    return x - y;
}





@interface ViewController ()

@property (nonatomic, copy) void (^block)(NSString *);


@property (weak, nonatomic) IBOutlet UILabel *lable;

@property (weak, nonatomic) IBOutlet TouchView *touchView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self functionReview];
    [self studyBlock];
    [self.touchView viewDidClick:^(TouchView *view) {
       
        NSLog(@"不让你碰");
        
        
    }];
    
    [self.touchView buttonHandle:^(UIButton *deleBtn) {
        NSLog(@"点击删除按键");
    }];
    
    [self.touchView editBtnHandle:^(UIButton *editBun) {

        NSLog(@"点击了编辑按键");
    } deleBtnHandle:^(UIButton *deleBtn) {
        NSLog(@"点击了删除按钮");
    }];
    
    [self handleData];

}

#pragma mark - 数据处理(网络类使用 Block 回调)

- (void)handleData {
    NSString *str = @"http://api.map.baidu.com/place/v2/search?query=银行&region=大连&output=json&ak=6E823f587c95f0148c19993539b99295";
    
    [NetworkingHandler nextworkingWithUrl:str completeHandle:^(NSURLResponse *response, NSData *data, id result) {
        NSLog(@"resp:%@,res%@",response,result);
    }];
}





#pragma mark - 知识点1: 复习函数
- (void)functionReview {
    
    /**
     * 函数由三部分: 函数声明, 函数实现, 函数调用
     *
     *
     *
     *
     */
    
    sum(3, 5);
    NSLog(@"%d",sum(3, 5));
    
    /** 
     *函数指针: 指针指向一块内存. 里面是一段代码段
     *
     */
    /** 定义一个函数指针变量 */
    int (*p)(int, int) = NULL;
    //函数指针变量赋值
    p = sum;
    p = sub;
    // 通过函数指针调用函数
    p(3, 5);
    NSLog(@"%d",p(3, 5));
    
    
    
    
    
    
}


#pragma mark - block
- (void)studyBlock {
    
    /** 
     * 1.block 和函数指针很相似.技巧: *p 换成 ^block
     * 2.Block 相当于匿名函数,所以 block 变量赋值时,直接赋函数的实现部分
     * 
     *
     */
    
    int (^block1) (int, int) = ^int(int x, int y)
    {
        return x + y;
    };
    
    
    
    
    // 调用 block
    block1(3, 5);
    NSLog(@"%d",block1(3, 5));
    
   // block 语法.
    // 无参无返 block
    void (^block2)(void) = ^(void) {
        
        NSLog(@"无参无返.");
        
        
    };
    block2();
    //  无参有返 block
    NSString* (^block3)(void) = ^NSString *(void){
    
    return @"无参有返";
        
    };
    block3();
    NSLog(@"%@",block3());
    
    //有参无返
    void (^block4)(NSString *, NSString *)=^ (NSString *str1, NSString *str2) {
        
        
        NSLog(@"%@",[str1 stringByAppendingString:str2]);
        
    };
    block4 (@"da",@"lian");
    
    // 有参有返
    NSString * (^block5)(NSString *, NSString *) = ^NSString * (NSString *str1, NSString *str2)
    {
        
        return [str1 stringByAppendingString:str2];
        
        
    };
   NSLog(@"%@",block5 (@"woaini",@"zhaoyue"));
    
    
    
    
    
    
    
    
    self.block = ^(NSString *str) {
        
        NSLog(@"%@",str);
        
        
    };
}

- (IBAction)handleBlock:(id)sender {
    
    // 调用 block
    
    self.block (@"aaa");
    
    
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    
    
    
    // 获取第二页面的对象
   SecondViewController *second = [segue destinationViewController];
    
    
    second.block = ^(NSString *str ){
        
        
        
        self.lable.text = str;
        
        
    };
    
    

    
    
    
    
}







#pragma mark -
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
